/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import event.JobAlarmEvent;
import event.JobConfirmEvent;
import event.JobRequestEvent;
import event.JobStatusEvent;
import net.sf.appia.core.AppiaEventException;
import net.sf.appia.core.Direction;
import net.sf.appia.core.Event;
import net.sf.appia.core.Layer;
import net.sf.appia.core.Session;
import net.sf.appia.core.events.channel.ChannelInit;
import util.Erro;
import util.Info;
import util.Status;

/**
 *
 * @author Welington
 */
public class TransformationSession extends Session {

    private int bound;
    private Class current = TransformationSession.class;

    /** Creates a new instance of BoundedPrintSession */
    public TransformationSession(Layer layer) {
        super(layer);
    }

    @Override
    public void handle(Event event) {
        if (event instanceof ChannelInit) {
            handleChannelInit((ChannelInit) event);
        } else if (event instanceof JobRequestEvent) {
            handleJobRequest((JobRequestEvent) event);
        } else if (event instanceof JobConfirmEvent) {
            handleJobConfirm((JobConfirmEvent) event);
        }
    }

    private void handleChannelInit(ChannelInit init) {
        try {
            bound = 5;

            init.go();
        } catch (AppiaEventException e) {
            Erro.showMessage(current, e);
        }
    }

    private void handleJobRequest(JobRequestEvent request) {
        if (bound > 0) {
            bound--;
            Info.showMessage(current, "Transforming request (" + request.getId() + ")");
            request.setString(request.getString().toUpperCase());
            try {
                request.go();
            } catch (AppiaEventException e) {
                Erro.showMessage(current, e);
            }
            if (bound == 0) {
                JobAlarmEvent alarm = new JobAlarmEvent();
                alarm.setChannel(request.getChannel());
                alarm.setDir(Direction.UP);
                alarm.setSourceSession(this);
                try {
                    alarm.init();
                    alarm.go();
                } catch (AppiaEventException e) {
                    Erro.showMessage(current, e);
                }
            }
        } else {
            JobStatusEvent status = new JobStatusEvent();
            status.setChannel(request.getChannel());
            status.setDir(Direction.UP);
            status.setSourceSession(this);
            status.setId(request.getId());
            status.setStatus(new Status(Status.StatusType.NOK));
            
            try {
                status.init();
                status.go();
            } catch (AppiaEventException e) {
                Erro.showMessage(current, e);
            }
        }
    }

    private void handleJobConfirm(JobConfirmEvent conf) {
        JobStatusEvent status = new JobStatusEvent();
        status.setId(conf.getId());
        status.setStatus(new Status(Status.StatusType.OK));

        Info.showMessage(current, "Received confirmation of request [" + conf.getId() + "]");
        
        try {
            bound++;
            status.setChannel(conf.getChannel());
            status.setDir(Direction.UP);
            status.setSourceSession(this);
            status.init();
            status.go();
        } catch (AppiaEventException e) {
            Erro.showMessage(current, e);
        }

    }
}
